// ==UserScript==
// @Propszt Istvan
// @Import/Export file  
// ==/UserScript==


function exportF() {
  
    const nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker); 
    fp.init(window, "Select a File", nsIFilePicker.modeSave);
    fp.appendFilter("iCalendar (*.ics)","*.ics");

    var rv = fp.show();
    
    if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
        var file = fp.file;
        var data = vcalendar + vtimezone;
        var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
               createInstance(Components.interfaces.nsIFileOutputStream);

        // use 0x02 | 0x10 to open file for appending.
        foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
        var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                createInstance(Components.interfaces.nsIConverterOutputStream);
        converter.init(foStream, "UTF-8", 0, 0);
        data = data + "END:VCALENDAR"
        converter.writeString(data);
        converter.close(); // this closes foStream
        
    }

}

function importF(){

    const nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker); 
    fp.init(window, "Select a File", nsIFilePicker.modeOpen);
    fp.appendFilter("iCalendar","*.ics");

    var rv = fp.show();
    
    if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
        var file = fp.file;
        var data = "";
        var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
              createInstance(Components.interfaces.nsIFileInputStream);
        var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
              createInstance(Components.interfaces.nsIConverterInputStream);
        fstream.init(file, -1, 0, 0);
        cstream.init(fstream, "UTF-8", 0, 0);
        let (str = {}){
            let read = 0;
            do { 
                read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
                data += str.value;
            } while (read != 0);
        }
        cstream.close(); // this closes fstream

        var Events = data.split("BEGIN:VEVENT");
        var tomb = new Array();

        for (var i = 1 ; i<Events.length ; i++){
            tomb.push(record());

            var j = Events[i].indexOf("SUMMARY:");
            tomb[i-1].summery = "";
//SUMMERY
            if (j > -1){
                j = j+8;
                while (Events[i][j] != "\n"){
                    tomb[i-1].summery = tomb[i-1].summery + (Events[i][j]);
                    j++;
                }
            }
//DESCRIPTION
            j = Events[i].indexOf("DESCRIPTION:");
            var k = Events[i].indexOf("BEGIN:VALARM");
            tomb[i-1].description = "";
            if (j > -1 && k > j){
                j = j+12;
                while (Events[i][j] != "\n"){
                    tomb[i-1].description = tomb[i-1].description + (Events[i][j]);
                    j++;
                }
            }
//LOCATION
            j = Events[i].indexOf("LOCATION:");
            tomb[i-1].location = "";
            if (j > -1){
                j = j+9;
                while (Events[i][j] != "\n"){
                    tomb[i-1].location = tomb[i-1].location + (Events[i][j]);
                    j++;
                }
            }
//CATEGORIES
            j = Events[i].indexOf("CATEGORIES:");
            tomb[i-1].categories = "";
            if (j > -1){
                j = j+11;
                while (Events[i][j] != "\n"){
                    tomb[i-1].categories = tomb[i-1].categories + (Events[i][j]);
                    j++;
                }
            }
//Start Date & Time
            j = Events[i].indexOf("DTSTART");
            if (j > -1){
                j = j+7;
                while (Events[i][j] != ":"){
                    j++;
                }
                j++;
         
                tomb[i-1].syear = parseInt(Events[i][j]+Events[i][j+1]+Events[i][j+2]+Events[i][j+3]);
                j = j+4;
                tomb[i-1].smonth = parseInt(Events[i][j]+Events[i][j+1]);
                j = j+2;
                tomb[i-1].sday = parseInt(Events[i][j]+Events[i][j+1]);
                j = j+2;
                if (Events[i][j] == "T"){
                    j++;
                    tomb[i-1].shour = parseInt(Events[i][j]+Events[i][j+1]);
                    j = j+2;
                    tomb[i-1].sminute = parseInt(Events[i][j]+Events[i][j+1]);
                }
                else{
                    tomb[i-1].shour = 0;
                    tomb[i-1].sminute = 0;
                }
              
            }
//End Date & Time
            j = Events[i].indexOf("DTEND");
            if (j > -1){
                j = j+5;
                while (Events[i][j] != ":"){
                    j++;
                }
                j++;
                tomb[i-1].eyear = parseInt(Events[i][j]+Events[i][j+1]+Events[i][j+2]+Events[i][j+3]);
                j = j+4;
                tomb[i-1].emonth = parseInt(Events[i][j]+Events[i][j+1]);
                j = j+2;
                tomb[i-1].eday = parseInt(Events[i][j]+Events[i][j+1]);
                j = j+2;
                if (Events[i][j] == "T"){
                    j++;
                    tomb[i-1].ehour = parseInt(Events[i][j]+Events[i][j+1]);
                    j = j+2;
                    tomb[i-1].eminute = parseInt(Events[i][j]+Events[i][j+1]);
                }
                else{
                    tomb[i-1].ehour = 0;
                    tomb[i-1].eminute = 0;
                }
              
            }


        }

        for (var i = 0 ; i<tomb.length ; i++){
            window.alert(tomb[i].summery+"   "+tomb[i].description+"    "+tomb[i].categories+"    "+tomb[i].syear+"-"+tomb[i].smonth+"-"+tomb[i].sday+"    "+tomb[i].shour+" : "+tomb[i].sminute+"    "+tomb[i].eyear+"-"+tomb[i].emonth+"-"+tomb[i].eday+"    "+tomb[i].ehour+" : "+tomb[i].eminute);
        }

    }
}

function record(){
    return {summary:"Nev",    //summary = Event name
            description:"leírás",
            location:"hely",
            categories:"kategoria",
            reminder:0,
            repeat:0,
            shour:12,         //s = Start
            sminute:01,
            syear:1990,
            smonth:1,
            sday:1,
            ehour:12,         //e = End
            eminute:01,
            eyear:1990,
            emonth:1,
            eday:1}
}

var vcalendar = "BEGIN:VCALENDAR\nPRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN\nVERSION:2.0\n";
var vtimezone = "BEGIN:VTIMEZONE\nTZID:Europe/Bucharest\nX-LIC-LOCATION:Europe/Bucharest\nBEGIN:DAYLIGHT\nTZOFFSETFROM:+0200\nTZOFFSETTO:+0300\nTZNAME:EEST\nDTSTART:19700329T030000\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\nEND:DAYLIGHT\nBEGIN:STANDARD\nTZOFFSETFROM:+0300\nTZOFFSETTO:+0200\nTZNAME:EET\nDTSTART:19701025T040000\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\nEND:STANDARD\nEND:VTIMEZONE\n";